<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewOwnservice extends JView
{
	function display($tpl=null){

		global $mainframe, $option;

		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();

		if($this->getLayout() == 'transfer') {
			$this->_displayTransfer($tpl);
			return;
		} 

		if($this->getLayout() == 'detail') {
			$this->_displayConfirmdetail($tpl);
			return;
		}
		
		if($this->getLayout() == 'message') {
			$this->_displayMessage($tpl);
			return;
		}

		if( $this->getLayout() == 'changepassword' ) {
			$this->_displayChangePassword($tpl);
			return;
		}

		$filter_order		= $mainframe->getUserStateFromRequest($option.'.ownservice.filter_order', 'filter_order', 'a.account_no', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.ownservice.filter_order_Dir', 'filter_order_Dir', '', 'cmd');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//Get data form the model
		$model				= $this->getModel();
		$accounts			= $model->getAccounts();

		$n = count($accounts);
		for($i=0; $i < $n; $i++)
		{
			$account =& $accounts[$i];
			
			switch($account->account_type)
			{
				case "saving":
					$account->account_type = 'savings';
				break;
			}
		}

		$lists = array();

		$lists['order']     = $filter_order;
		$lists['order_Dir'] = $filter_order_Dir;

		$this->assignRef('totalTransfer', $totalTransfer);
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('accounts', $accounts);
		$this->assignRef('lists', $lists);

		parent::display($tpl);
	}

	function _displayTransfer($tpl) {
		global $mainframe;

		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();

		JHTML::_('behavior.mootools');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//get javascript
		$document->addScript(JURI::base().'/components/com_ownservice/assets/js/ownservice.js');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		
		//get vars
		$cid = JRequest::getVar('cid', array(0), 'post', 'array');

		//Get data form the model
		$model		= $this->getModel();
		$accounts	= $model->getAccounts($cid);
		$owndata	= $model->getOwndata();

		//check can transfer
		if(!$model->canTransfer()) {
			JError::raiseWarning(500, $owndata->id_type.' '.JText::_('OWNSERVICE_EXPIRE'));
			$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			exit();
		}

		$accountlist = array();
		if($accounts) {
			foreach($accounts as $account) {
				$accountlist[]		= JHTML::_( 'select.option', $account->account_no,OwnserviceHelper::accountFormat($account->account_no).' '.$account->account_name);
			}
		}

		$reasonlist = array();
		$reasonlist[] = JHTML::_( 'select.option', '',											ucwords(strtolower(JText::_('OWNSERVICE_SELECT_REASON')))	);
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('GIFT')),					ucwords(strtolower(JText::_('OWNSERVICE_GIFT'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Monthly Expenses')),		ucwords(strtolower(JText::_('OWNSERVICE_MONTHLY_EXPENSES'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Electricity Bill')),		ucwords(strtolower(JText::_('OWNSERVICE_ELECTRICITY_BILL'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Water bill')),			ucwords(strtolower(JText::_('OWNSERVICE_WATER_BILL'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('RENT')),					ucwords(strtolower(JText::_('OWNSERVICE_RENT'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('SHOPPING')),				ucwords(strtolower(JText::_('OWNSERVICE_SHOPPING'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Personal Savings')),		ucwords(strtolower(JText::_('OWNSERVICE_PERSONAL_SAVINGS'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('AIR FARE')),				ucwords(strtolower(JText::_('OWNSERVICE_AIR_FARE'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('TRAVEL')),				ucwords(strtolower(JText::_('OWNSERVICE_TRAVEL'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('MEDICAL')),				ucwords(strtolower(JText::_('OWNSERVICE_MEDICAL'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('EDUCATION')),				ucwords(strtolower(JText::_('OWNSERVICE_EDUCATION'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Car Installment')),		ucwords(strtolower(JText::_('OWNSERVICE_CAR_INSTALLMENT'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Car Loan Repayment')),	ucwords(strtolower(JText::_('OWNSERVICE_CAR_LOAN_REPAYMENT'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('House Intallment')),		ucwords(strtolower(JText::_('OWNSERVICE_HOUSE_INTALLMENT'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Home Loan Repayment')),	ucwords(strtolower(JText::_('OWNSERVICE_HOME_LOAN_REPAYMENT'))) );
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('other')),					ucwords(strtolower(JText::_('OWNSERVICE_OTHER'))) );

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('accounts', $accounts);
		$this->assignRef('owndata', $owndata);
		$this->assignRef('accountlist', $accountlist);
		$this->assignRef('reasonlist', $reasonlist);

		parent::display($tpl);
	}

	function _displayConfirmdetail($tpl) {
		
		global $mainframe, $option;

		//Initialise variables
		$session		= & JFactory::getSession();
		$user			= & JFactory::getUser();
		$db			= & JFactory::getDBO();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();

		JHTML::_('behavior.mootools');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addScript(JURI::base().'/components/com_ownservice/assets/js/ownservice.js');

		//get data
		$rows		= array();
		$rows		= $session->get('datatransfer', array(), $option);
		$model	= $this->getModel();
		if($rows)
		{
			foreach($rows['account'] as $i=>$row)
			{
					$rows['accountdetail'][$i]=$model->getAccountDetail($row);
			}
		}

		$owndata	= $this->get('Owndata');

		//assign data to document
		$this->assignRef('rows', $rows);
		$this->assignRef('owndata', $owndata);
		
		parent::display($tpl);
	}

	function _displayMessage($tpl) {

		//Initialise variables
		$user = & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document = & JFactory::getDocument();

		//add css to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//get data form the midel
		$model		= $this->getModel();
		$totalTransfer	= $this->get('TotalTransfer');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('totalTransfer', $totalTransfer);

		parent::display($tpl);
	}

	function _displayChangePassword($tpl) {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$gid		= $user->get('id');
		$acl		= & JFactory::getACL();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);

		parent::display($tpl);
	}
}